package com.example.mystore.service;

import com.example.mystore.model.Product;
import com.example.mystore.model.Category;
import com.example.mystore.repository.ProductRepository;
import com.example.mystore.repository.CategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;

    // 获取推荐商品
    public List<Product> getFeaturedProducts() {
        // 这里可以实现获取推荐商品的逻辑，例如按销量、评分等排序
        return productRepository.findAll();
    }

    @Autowired
    private CategoryRepository categoryRepository;

    // 获取所有商品
    public List<Product> getAllProducts() {
        return productRepository.findAll();
    }

    // 获取指定商品
    public Product getProductById(Long id) {
        return productRepository.findById(id).orElse(null);
    }

    // 添加商品
    public void addProduct(String name, double price, int stock, Long category, MultipartFile image, String description) {
        Category cat = categoryRepository.findById(category).orElse(null);
        if (cat == null) {
            return;
        }

        Product product = new Product();
        product.setName(name);
        product.setPrice(price);
        product.setStock(stock);
        product.setCategoryId(cat.getId());
        product.setDescription(description);

        // 保存图片
        if (!image.isEmpty()) {
            try {
                String imagePath = saveImage(image);
                product.setImage(imagePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        productRepository.save(product);
    }

    // 更新商品
    public void updateProduct(Long id, String name, double price, int stock, Long category, MultipartFile image, String description) {
        Product product = productRepository.findById(id).orElse(null);
        if (product == null) {
            return;
        }

        product.setName(name);
        product.setPrice(price);
        product.setStock(stock);
        product.setCategoryId(category);
        product.setDescription(description);

        // 更新图片
        if (!image.isEmpty()) {
            try {
                String imagePath = saveImage(image);
                product.setImage(imagePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        productRepository.save(product);
    }

    // 删除商品
    public void deleteProduct(Long id) {
        productRepository.deleteById(id);
    }

    // 获取分类下的商品
    public List<Product> getProductsByCategory(Long categoryId) {
        return productRepository.findByCategoryId(categoryId, Sort.by("id"));
    }

    // 保存图片
    private String saveImage(MultipartFile file) throws IOException {
        // 实际项目中应使用更安全的路径和文件名处理
        String filePath = "images/" + file.getOriginalFilename();
        File dest = new File(filePath);
        if (!dest.exists()) {
            dest.getParentFile().mkdirs();
        }
        file.transferTo(dest);
        return filePath;
    }
}
